package com.ruoyi.project.mall.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.mall.domain.MallShopTypeDO;
import com.ruoyi.project.mall.domain.MallShopUserDO;
import com.ruoyi.project.mall.service.MallShopTypeService;
import com.ruoyi.project.mall.service.MallShopUserService;
import com.ruoyi.project.shop.domain.ShopShopDO;
import com.ruoyi.project.shop.service.ShopShopService;
import com.ruoyi.project.shop.service.ShopUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 商家入驻信息
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-12-25 10:18:47
 */
 
@Controller
@RequestMapping("/mall/mallShopUser")
public class MallShopUserController extends BaseController {
	@Autowired
	private MallShopUserService mallShopUserService;
	@Autowired
	private MallShopTypeService mallShopTypeService;
	@Autowired
	private ShopUserService shopUserService;
	@Autowired
	private ShopShopService shopShopService;


	/**
	 * 查询商家入驻信息列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallShopUser:list')")
	@GetMapping("/list")
	public TableDataInfo list(MallShopUserDO mallShopUser)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(mallShopUser);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","ca_at");
		stringObjectMap.put("order","desc");
		List<MallShopUserDO> mallShopUserList = mallShopUserService.likeList(stringObjectMap);
		if(mallShopUserList.size()>0){
			for (MallShopUserDO mallShopUserDO : mallShopUserList) {
				MallShopTypeDO mallShopTypeDO = mallShopTypeService.get(mallShopUserDO.getIndustryType());
				MallShopTypeDO mallShopTypeDO1 = mallShopTypeService.get(mallShopUserDO.getIndustryAttribute());
				if(mallShopTypeDO!=null){
					mallShopUserDO.setIndustryType(mallShopTypeDO.getName());
				}else{
					mallShopUserDO.setIndustryType("");
				}
				if(mallShopTypeDO1!=null){
					mallShopUserDO.setIndustryAttribute(mallShopTypeDO1.getName());
				}else{
					mallShopUserDO.setIndustryAttribute("");
				}

			}
		}
		return getDataTable(mallShopUserList);
	}

	/**
	 * 导出商家入驻信息列表
	 */
	@PreAuthorize("@ss.hasPermi('mall:mallShopUser:export')")
	@Log(title = "商家入驻信息", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MallShopUserDO mallShopUser)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(mallShopUser);
		List<MallShopUserDO> list = mallShopUserService.list(stringObjectMap);
		ExcelUtil<MallShopUserDO> util = new ExcelUtil<MallShopUserDO>(MallShopUserDO.class);
		return util.exportExcel(list, "mallShopUser");
	}

	/**
	 * 获取商家入驻信息详细信息
	 */
	@PreAuthorize("@ss.hasPermi('mall:mallShopUser:query')")
	@GetMapping(value = "/{id}")
	@ResponseBody
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		return AjaxResult.success(mallShopUserService.get(id));
	}

	/**
	 * 新增商家入驻信息
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallShopUser:add')")
	@Log(title = "商家入驻信息", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MallShopUserDO mallShopUser)
	{
		mallShopUser.setId(IdUtil.simpleUUID());
		mallShopUser.setDelFlag(0);
		mallShopUser.setCaAt(new Date());
		return toAjax(mallShopUserService.save(mallShopUser));
	}

	/**
	 * 修改商家入驻信息
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallShopUser:edit')")
	@Log(title = "商家入驻信息", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MallShopUserDO mallShopUser)
	{
		mallShopUser.setUpAt(new Date());
		return toAjax(mallShopUserService.update(mallShopUser));
	}

	/**
	 * 删除商家入驻信息
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallShopUser:remove')")
	@Log(title = "商家入驻信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				MallShopTypeDO mallShopType = mallShopTypeService.get(string);
				mallShopType.setDelFlag(1);
				mallShopType.setUpAt(new Date());
				mallShopTypeService.update(mallShopType);
				ShopShopDO shopShopDO = shopShopService.get(mallShopType.getId());
				if(shopShopDO!=null){
					shopShopDO.setDelFlag(1);
					shopShopService.update(shopShopDO);
				}
			}
		}
		return toAjax(ids.length);
		//return toAjax(mallShopUserService.deleteMallShopUserByIds(ids));
	}

	
	/*@GetMapping()
	@RequiresPermissions("mall:mallShopUser:mallShopUser")
	String MallShopUser(){
	    return "mall/mallShopUser/mallShopUser";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("mall:mallShopUser:mallShopUser")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&& StringUtils.isNotBlank(params.get("name").toString())){
            List<MallShopUserDO> mallShopUserList = mallShopUserService.likeList(query);
            if(mallShopUserList.size()>0){
				for (MallShopUserDO mallShopUserDO : mallShopUserList) {
					MallShopTypeDO mallShopTypeDO = mallShopTypeService.get(mallShopUserDO.getIndustryType());
					MallShopTypeDO mallShopTypeDO1 = mallShopTypeService.get(mallShopUserDO.getIndustryAttribute());
					if(mallShopTypeDO!=null){
						mallShopUserDO.setIndustryType(mallShopTypeDO.getName());
					}else{
						mallShopUserDO.setIndustryType("");
					}
					if(mallShopTypeDO1!=null){
						mallShopUserDO.setIndustryAttribute(mallShopTypeDO1.getName());
					}else{
						mallShopUserDO.setIndustryAttribute("");
					}

				}
			}
            int total = mallShopUserService.likeListCount(query);
            PageUtils pageUtils = new PageUtils(mallShopUserList, total);
            return pageUtils;
		}

		List<MallShopUserDO> mallShopUserList = mallShopUserService.list(query);
		if(mallShopUserList.size()>0){
			for (MallShopUserDO mallShopUserDO : mallShopUserList) {
				MallShopTypeDO mallShopTypeDO = mallShopTypeService.get(mallShopUserDO.getIndustryType());
				MallShopTypeDO mallShopTypeDO1 = mallShopTypeService.get(mallShopUserDO.getIndustryAttribute());
				if(mallShopTypeDO!=null){
					mallShopUserDO.setIndustryType(mallShopTypeDO.getName());
				}else{
					mallShopUserDO.setIndustryType("");
				}
				if(mallShopTypeDO1!=null){
					mallShopUserDO.setIndustryAttribute(mallShopTypeDO1.getName());
				}else{
					mallShopUserDO.setIndustryAttribute("");
				}

			}
		}
		int total = mallShopUserService.count(query);
		PageUtils pageUtils = new PageUtils(mallShopUserList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("mall:mallShopUser:add")
	String add(){
	    return "mall/mallShopUser/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("mall:mallShopUser:edit")
	String edit(@PathVariable("id") String id,Model model){
		MallShopUserDO mallShopUser = mallShopUserService.get(id);
		model.addAttribute("mallShopUser", mallShopUser);
	    return "mall/mallShopUser/edit";
	}
	
	*//**
	 * 保存
	 *//*
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("mall:mallShopUser:add")
	public R save(MallShopUserDO mallShopUser){
	    mallShopUser.setId(UuidUtil.get32UUID());
	    mallShopUser.setDelFlag(0);
	    mallShopUser.setCaAt(new Date());
		if(mallShopUserService.save(mallShopUser)>0){
			return R.ok();
		}
		return R.error();
	}
	*//**
	 * 修改
	 *//*
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("mall:mallShopUser:edit")
	public R update( MallShopUserDO mallShopUser){
		mallShopUser.setUpAt(new Date());
		mallShopUserService.update(mallShopUser);
		return R.ok();
	}
	
	*//**
	 * 删除
	 *//*
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("mall:mallShopUser:remove")
	public R remove( String id){
		MallShopUserDO mallShopUser = mallShopUserService.get(id);
		mallShopUser.setDelFlag(1);
		mallShopUser.setUpAt(new Date());
		if(mallShopUserService.update(mallShopUser)>0){
		return R.ok();
		}
		return R.error();
	}
	
	*//**
	 * 删除
	 *//*
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("mall:mallShopUser:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				MallShopUserDO mallShopUser = mallShopUserService.get(string);
				mallShopUser.setDelFlag(1);
				mallShopUser.setUpAt(new Date());
				mallShopUserService.update(mallShopUser);
			}
		}
	
		return R.ok();
	}


	*//**
	 * 审核
	 *//*
	@ResponseBody
	@PostMapping("/shen")
	@RequiresPermissions("mall:mallShopUser:edit")
	public R shen(MallShopUserDO mallShopUser){
		MallShopUserDO mallShopUserDO = mallShopUserService.get(mallShopUser.getId());
		if(mallShopUserDO.getStatus()==1){
			return R.error("已经申请通过");
		}
		mallShopUserService.update(mallShopUser);
		return R.ok();
	}

	@PreAuthorize("@ss.hasPermi('mall:mallShopUser:add')")
	@Log(title = "商家入驻信息", businessType = BusinessType.INSERT)
	@PostMapping
	String getShopUser(@PathVariable("id") String id,Model model){
		ShopUserDO shopUserDO = shopUserService.getShopAdminByShopId(id);
		if(shopUserDO==null){
			shopUserDO = new ShopUserDO();
			shopUserDO.setDelFlag("0");
		}
		model.addAttribute("shopUser", shopUserDO);
		model.addAttribute("shopId", id);
		return "shop/shopUser/edit";
	}*/
	
}
